﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;

namespace WPFExcelReport
{
    public partial class TAV13AB05 : ViewBase
    {
        /*#region ==FROMDATE===========
        public static readonly DependencyProperty FROMDATEProperty = DependencyProperty.Register("FROMDATE",
            typeof(string), typeof(TAV13AB05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFROMDATEChanged));
        public string FROMDATE
        {
            get
            {
                return (string)GetValue(FROMDATEProperty);
            }
            set
            {
                SetValue(FROMDATEProperty, value);
            }
        }
        private static void OnFROMDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAV13AB05;
            if (src != null)
            {
                src.FROMDATE = (string)e.NewValue;
            }
        }
        #endregion
        #region ==ART===========
        public static readonly DependencyProperty ARTProperty = DependencyProperty.Register("ART",
            typeof(string), typeof(TAV13AB05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnARTChanged));
        public string ART
        {
            get
            {
                return (string)GetValue(ARTProperty);
            }
            set
            {
                SetValue(ARTProperty, value);
            }
        }
        private static void OnARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAV13AB05;
            if (src != null)
            {
                src.ART = (string)e.NewValue;
            }
        }
        #endregion*/
        /*#region ==ENDDATE===========
        public static readonly DependencyProperty ENDDATEProperty = DependencyProperty.Register("ENDDATE",
            typeof(string), typeof(TAV13AB05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnENDDATEChanged));
        public string ENDDATE
        {
            get
            {
                return (string)GetValue(ENDDATEProperty);
            }
            set
            {
                SetValue(ENDDATEProperty, value);
            }
        }
        private static void OnENDDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAV13AB05;
            if (src != null)
            {
                src.ENDDATE = (string)e.NewValue;
            }
        }
        #endregion
        */
        public string FROMDATE = "", ENDDATE = "",ART = "";
        #region ==dwAV13AB05===========
        public static readonly DependencyProperty dwAV13AB05Property = DependencyProperty.Register("dwAV13AB05",
            typeof(DataRow), typeof(TAV13AB05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwAV13AB05Changed));
        public DataRow dwAV13AB05
        {
            get
            {
                return (DataRow)GetValue(dwAV13AB05Property);
            }
            set
            {
                SetValue(dwAV13AB05Property, value);
            }
        }
        private static void OndwAV13AB05Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAV13AB05;
            if (src != null)
            {
                src.dwAV13AB05 = (DataRow)e.NewValue;
            }
        }
        #endregion
        VABPOS GB;
        ITXEXRPT ITXEXRPT;
        public TAV13AB05()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABJOUR();
            GB = new VABPOS();
            IsMasterFromMV = true;
            TBMain = MV.SetProd.Tables[TB];  
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            base.Window_Loaded(sender, e);
        }        
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                QueryCommand(null, null);
            }
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            DataSet Result;
            DataSet Return = new DataSet();
            DataRow dwData = null;
            ART = txtART.Text.ToString().Trim();
            if (FROMDATE == "")
            {
                FROMDATE ="20000000";
            }
            if (ENDDATE=="")
            {
                ENDDATE = "99999999";
            }
            dwData = GB.SetProd.Tables["ABPOS"].NewRow();
            dwData["ABPART"] = ART;
            dwData["ABPNEUDAT"] = FROMDATE;
            dwData["ABPAENDDAT"] = ENDDATE;
            try
            {
                Result = ((VABJOUR)MV).QueryABJOUR(dwData, 1);
                MergeData(Result, true, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            base.QueryCommand(sender, e);
        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwAV13AB05 = dwMaster;
        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = MV.SetProd;
                //ITXEXRPT.dsSource.Merge(SetEnv);
                
                ITXEXRPT.DocType = "JLTAV13AB05";
                
                ITXEXRPT.strExcelFile = @"JLT\JLTAV13AB05.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
